package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.po.application.ApplicationPo;
import com.bwda.dsrs.filecenter.domain.condition.application.ApplicationCondition;
import com.bwda.dsrs.filecenter.domain.form.application.ApplicationQueryForm;
import com.bwda.dsrs.filecenter.domain.form.application.ApplicationUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.application.ApplicationCreateForm;
import com.bwda.dsrs.filecenter.domain.po.applicationkeyhistory.ApplicationKeyhistoryPo;
import com.bwda.dsrs.filecenter.domain.vo.application.ApplicationVo;
import com.bwda.dsrs.filecenter.service.ApplicationKeyhistoryService;
import com.bwda.dsrs.filecenter.service.ApplicationService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.util.PasswordBuilder;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/application")
@Api(value = "/b/application", tags = "应用系统管理")
public class ApplicationController extends BaseController {

	@Autowired
	private ApplicationService applicationService;
	@Autowired
	private ApplicationKeyhistoryService applicationKeyhistoryService;



	@ApiOperation(value = "查询应用系统管理",notes = "查询应用系统管理",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<ApplicationVo> query(@ApiParam(value = "应用系统id", required = true)@RequestParam Integer appId) throws BusinessServiceException {
		ApplicationPo po = applicationService.queryWithValid(appId);
		ApplicationVo vo = CopyUtil.transfer(po, ApplicationVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询应用系统管理数量",notes = "查询应用系统管理数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid ApplicationQueryForm form) throws BusinessServiceException {
		ApplicationCondition condition = this.getConditionByQueryForm(form);
		int count = applicationService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询应用系统管理列表",notes = "查询应用系统管理列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<ApplicationVo>> queryList(@ModelAttribute@Valid ApplicationQueryForm form) throws BusinessServiceException {
		ApplicationCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<ApplicationPo> poList = applicationService.queryList(condition);
		List<ApplicationVo> voList = CopyUtil.transfer(poList, ApplicationVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询应用系统管理列表(带分页)",notes = "查询应用系统管理列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<ApplicationVo>> queryPageList(@ModelAttribute@Valid ApplicationQueryForm form) throws BusinessServiceException {
		ApplicationCondition condition = this.getConditionByQueryForm(form);
		List<ApplicationVo> voList = new ArrayList<>();
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		int count = applicationService.queryCount(condition);
		if (count > BusinessConstants.NumberConstant.ZERO) {
			List<ApplicationPo> poList = applicationService.queryList(condition);
			voList = CopyUtil.transfer(poList, ApplicationVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增应用系统管理",notes = "新增应用系统管理",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<ApplicationVo> add(@ModelAttribute@Valid ApplicationCreateForm form) throws BusinessServiceException {
		ApplicationPo po = CopyUtil.transfer(form, ApplicationPo.class);
//		po.setAppId(RandomUtils.nextInt());
		po.setDelFlag(0);
		po.setCreatedTime(new Date());
		po.setUpdateTime(new Date());
		//判断应用系统编码是否重名,存在则返回false
		boolean bExist = applicationService.queryAppCode(po.getAppCode());
		if(bExist==false){
			return getFailResult("应用系统编码重复，请重新输入编码!");
		}
		String  pwdKey = PasswordBuilder.createPwd(16,32,true, true, true,true);
		po.setAppSecret(pwdKey);
		applicationService.insert(po);
		ApplicationVo vo = CopyUtil.transfer(po, ApplicationVo.class);
		return getSuccessResult(vo);
	}

	/**
	 * 针对普通编辑修改
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@ApiOperation(value = "修改应用系统管理",notes = "修改应用系统管理",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid ApplicationUpdateForm form) throws BusinessServiceException {
		ApplicationPo po = CopyUtil.transfer(form, ApplicationPo.class);
		//修改时间
		po.setUpdateTime(new Date());
		applicationService.update(po);
		return getSuccessResult();
	}

	/**
	 * 针对秘钥修改
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@ApiOperation(value = "修改应用系统管理密钥",notes = "修改应用系统管理密钥",httpMethod = "POST")
	@RequestMapping(value = "/updateKey", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity updateKey(@ModelAttribute@Valid ApplicationUpdateForm form) throws BusinessServiceException {
		ApplicationPo po = CopyUtil.transfer(form, ApplicationPo.class);
		//获取注册密钥Id
		Integer appId = po.getAppId();
		ApplicationPo application = applicationService.query(appId);
		ApplicationKeyhistoryPo appKeyhistoryPo = new ApplicationKeyhistoryPo();
		//修改时间
		application.setUpdateTime(new Date());
		String  pwdKey = PasswordBuilder.createPwd(16,32,true, true, true,true);
		if(application!=null){
			//应用系统id
			appKeyhistoryPo.setAppId(appId);
			//更新方式0自动更新
			appKeyhistoryPo.setUpdateType(0);
			//应用系统编码
			appKeyhistoryPo.setAppCode(application.getAppCode());
			//应用系统原始秘钥
			appKeyhistoryPo.setApplicationSecretKey(application.getAppSecret());
			//拼接后秘钥(最新秘钥)
			appKeyhistoryPo.setApplicationSecretKeyInfo(pwdKey);
			application.setAppSecret(pwdKey);
			appKeyhistoryPo.setCreateTime(application.getCreatedTime());
			appKeyhistoryPo.setUpdateTime(new Date());
			applicationKeyhistoryService.insert(appKeyhistoryPo);
		}
		applicationService.update(application);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除应用系统管理",notes = "删除应用系统管理",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "应用系统id", required = true)@RequestParam List<Integer> appId) throws BusinessServiceException {
		logger.info("删除应用系统管理");
		//支持批量
		appId.forEach(integer -> {
			try {
				applicationService.delete(integer);
			} catch (BusinessServiceException e) {
				e.printStackTrace();
			}
		});
		return getSuccessResult();
	}

	/**
	 * ApplicationQueryForm转换为ApplicationCondition
	 *
	 * @param form
	 * @return
	 */
	private ApplicationCondition getConditionByQueryForm(ApplicationQueryForm form) {
		ApplicationCondition condition = CopyUtil.transfer(form, ApplicationCondition.class);
		return condition;
	}

}